using AutoMapper;
using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.SimpleTemp.Application.UserApp;
using Core.SimpleTemp.Entitys;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using Core.Toolkit.Core.Application;
using Core.SimpleTemp.Repositories.Internal;

namespace Core.SimpleTemp.Api.Api
{
    /// <summary>
    /// CBS_BreakRulesApprove
    ///</summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class CBS_BreakRulesApproveController : CoreApiController
    {
        private readonly SysUserAppService _userService;
        private readonly CBS_BreakRulesApproveService _service;
        private readonly CoreDBContext _dbContext;
        /// <summary>
        /// CBS_BreakRulesApprove
        ///</summary>
        public CBS_BreakRulesApproveController(CBS_BreakRulesApproveService service, SysUserAppService userService, CoreDBContext coreDBContext)
        {
            _service = service;
            _userService = userService;
            _dbContext = coreDBContext;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        [PermissionFilter(nameof(CBS_BreakRulesApproveController) + "_" + nameof(CBS_BreakRulesApproveController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
            GetList(PagingModel<CBS_BreakRulesApproveDto>
                pagingQueryModel)
        {
            IPageModel<CBS_BreakRulesApproveDto>
                result;
            result = await _service.GetList(pagingQueryModel);
            return this.JsonSuccess(result);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("GetApproveList")]
        [PermissionFilter(nameof(CBS_BreakRulesApproveController) + "_" + nameof(CBS_BreakRulesApproveController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
    GetApproveList(PagingModel<CBS_BreakRulesApproveDto>
        pagingQueryModel)
        {
            IPageModel<CBS_BreakRulesApproveDto>
                result;
            result = await _service.GetApproveList(pagingQueryModel);
            return this.JsonSuccess(result);
        }


        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(CBS_BreakRulesApproveController) + "_" + nameof(CBS_BreakRulesApproveController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(CBS_BreakRulesApproveDto dto)
        {
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                await _service.UpdateAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
        }


        /// <summary>
        /// 审批同意
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Agreed")]
        [PermissionFilter(nameof(CBS_BreakRulesApproveController) + "_" + nameof(CBS_BreakRulesApproveController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            AgreedAsync(CBS_BreakRulesApproveDto dto)
        {
            dto.ExamineUser = UserIdentity.Id;
            dto.ExamineState = "1";
            dto.ExamineDate = DateTime.Now;
            await _service.UpdateAsync(dto);

            //var newEntity = await _coreDBContext.
            var newEntity = Mapper.Map<List<CBS_BreakRules>>(dto.BreakRulesId);

            newEntity.ForEach(e =>
            {
                if (dto.ExamineOrder == 1)
                {
                    e.State = "3";
                }
                if (dto.ExamineOrder == 2)
                {
                    e.State = "4";
                }
                if (dto.ExamineOrder == 2 && dto.ExaminUserType == "04")
                {
                    e.State = "5";
                }
                if (dto.ExamineOrder == 3)
                {
                    e.State = "6";
                }
            });
            await _dbContext.AddRangeAsync(newEntity);
            await _dbContext.SaveChangesAsync();

            return this.JsonSuccess(string.Empty);
        }


        /// <summary>
        /// 审批不同意
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("NoAgreed")]
        [PermissionFilter(nameof(CBS_BreakRulesApproveController) + "_" + nameof(CBS_BreakRulesApproveController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            NoAgreedAsync(CBS_BreakRulesApproveDto dto)
        {
            dto.ExamineUser = UserIdentity.Id;
            dto.ExamineState = "2";
            dto.ExamineDate = DateTime.Now;
            await _service.UpdateAsync(dto);
            return this.JsonSuccess(string.Empty);
        }


        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(CBS_BreakRulesApproveController) + "_" + nameof(CBS_BreakRulesApproveController.DeleteAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult>
              DeleteAsync([FromBody]Guid id)
        {

            await DeleteBatchAsync(id);
            return this.JsonSuccess();
        }

        /// <summary>
        /// DeleteMuti
        /// </summary>
        /// <param name="id"></param>
        /// <param name="autoSave"></param>
        /// <returns></returns>
        private  Task DeleteBatchAsync(Guid id, bool autoSave = true)
        {
            return _service.DeleteAsync(it => id==(it.BreakRulesId), autoSave);
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult>
            Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            CBS_BreakRulesApproveDto CBS_BreakRulesApproveDto = Mapper.Map<CBS_BreakRulesApproveDto>(model);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            var result = new
            {
                cbs_breakRulesApprove = CBS_BreakRulesApproveDto
            };
            return this.JsonSuccess(result);
        }


        /// <summary>
        /// 导出
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("Export")]
        [PermissionFilter(nameof(CBS_BreakRulesApproveController) + "_" + nameof(CBS_BreakRulesApproveController.Export), AuthorizationConst.EXPORT)]
        public async Task<IActionResult>
            Export(PagingModel<CBS_BreakRulesApproveDto>
                pagingQueryModel)
        {
            var fileName = await _service.Export(pagingQueryModel);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }
        #endregion


    }
}
